import base64
import calendar
import datetime
import hashlib
import os.path
import random
import re
import time
from hashlib import sha1
from conf.setting import DIR_BASE
from common.operationcsv import read_csv
from common.readyaml import ReadYamlData
import csv


class DebugTalk:
    def __init__(self):
        self.read = ReadYamlData()

    def get_extract_order_data(self, data, randoms):
        """获取extract.yaml数据，不为0、-1、-2时，则按顺序读取文件key的数据"""
        if randoms not in [0, -1, -2]:
            return data[randoms - 1]

    def md5_encryption(self, params):
        """参数md5加密"""
        # 生成一个 MD5 哈希对象
        enc_data = hashlib.md5()

        # params.encode：把输入的参数转换为 utf-8 编码的字节串
        # enc_data.update：把字符串内容更新到MD5对象中
        # 由于 MD5 算法只能处理字节数据，所以必须先对字符串进行编码
        enc_data.update(params.encode(encoding="utf-8"))
        # 以十六进制字符串的形式返回 MD5 哈希值
        return enc_data.hexdigest()

    def sha1_encryption(self, params):
        """参数sha1加密"""
        enc_data = sha1()
        enc_data.update(params.encode(encoding="utf-8"))
        return enc_data.hexdigest()

    def base64_encryption(self, params):
        """base64加密"""
        base_params = params.encode("utf-8")
        encr = base64.b64encode(base_params)
        return encr

    def timestamp(self):
        """获得当前时间戳，10位"""
        t = int(time.time())
        return t

    def timestamp_thirteen(self):
        """获取当前时间戳，13位"""
        t = int(time.time()) * 1000
        return t

    def start_time(self):
        """获取当前时间的前一天标准时间"""
        now_time = datetime.datetime.now()
        # 把时间对象格式化为字符串
        day_before_time = (now_time - datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
        return day_before_time

    def end_time(self):
        """获取当前时间的标准时间格式"""
        now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        return now_time

    def end_year_time(self):
        """获取当前时间的标准时间格式，年月日"""
        now_time = datetime.datetime.now().strftime("%Y-%m-%d")
        return now_time

    def today_zero_tenstamp(self):
        """获取当天00:00:00的时间戳，10位时间戳"""
        # .timetuple()：将日期对象转换为时间元组
        # time.mktime：将时间元组转换为时间戳
        time_stamp = int(time.mktime(datetime.date.today().timetuple()))
        return time_stamp

    def today_zero_stamp(self):
        """获取当天00:00:00的时间戳，13位时间戳"""
        # .timetuple()：将日期对象转换为时间元组
        # time.mktime：将时间元组转换为时间戳
        time_stamp = int(time.mktime(datetime.date.today().timetuple())) * 1000
        return time_stamp

    def start_forward_time(self):
        """获取当前时间的前15天标准时间，年月日"""
        now_time = datetime.datetime.now()
        # 把时间对象格式化为字符串
        day_before_time = (now_time - datetime.timedelta(days=15)).strftime("%Y-%m-%d")
        return day_before_time

    def start_after_time(self):
        """获取当前时间后7天的标准时间，年月日"""
        now_time = datetime.datetime.now()
        # 把时间对象格式化为字符串
        day_after_time = (now_time + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
        return day_after_time

    def specified_zero_tamp(self, days):
        """获取当前日期+-days的00：00：00的时间戳，13位"""
        tom = datetime.date.today() + datetime.timedelta(days=int(days))
        date_tamp = int(time.mktime(time.strptime(str(tom), '%Y-%m-%d'))) * 1000
        return date_tamp

    def specified_end_tamp(self, days):
        """获取当前日期+-days的23:59:59的时间戳，13位"""
        tom = datetime.date.today() + datetime.timedelta(days=int(days) + 1)
        date_tamp = int(time.mktime(time.strptime(str(tom), '%Y-%m-%d'))) - 1
        return date_tamp * 1000

    def today_end_stamp(self):
        """获取今天23：59：59的时间戳，13位"""
        tomorrow = datetime.date.today() + datetime.timedelta(days=1)
        today_end_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) - 1
        return today_end_time * 1000

    def month_start_time(self):
        """获取本月第一天标准时间，年月日"""
        now = datetime.datetime.now()
        this_month_start = datetime.datetime(now.year, now.month, 1).strftime('%Y-%m-%d')
        return this_month_start

    def month_end_time(self):
        """获取本月最后一天标准时间，年月日"""
        now = datetime.datetime.now()
        this_month_end = datetime.datetime(now.year, now.month, calendar.monthrange(now.year, now.month)[1]).strftime(
            '%Y-%m-%d')
        return this_month_end

    def month_first_time(self):
        """获取本月1号00：00：00时间戳，13位"""
        # 今日日期
        now = datetime.datetime.now()
        # 本月1号日期
        this_month_start = datetime.datetime(now.year, now.month, 1)
        first_time_stamp = int(time.mktime(this_month_start.timetuple())) * 1000
        return first_time_stamp

    def fenceAlarm_alarmType_random(self):
        alarm_type = ["1", "3", "8", "2", "5", "6"]
        fence_alarm = random.choice(alarm_type)
        return fence_alarm

    def fatigueAlarm_alarmType_random(self):
        alarm_type = ["1", "3", "8"]
        fatigue_alarm = random.choice(alarm_type)
        return fatigue_alarm

    def jurisdictionAlarm_random(self):
        alarm_type = ["1", "3", "8", "2", "5", "6", "9"]
        jurisdiction_alarm = random.choice(alarm_type)
        return jurisdiction_alarm

    def vehicle_random(self):
        """从csv中随机读取车牌号"""
        # 路径：DIR_BASE\data\vehicleNo.csv
        data = read_csv(os.path.join(DIR_BASE, 'data', 'vehicleNo.csv'), 'vno')
        vel_num = random.choice(data)
        return vel_num

    def read_csv_data(self, file_name, index):
        """读取csv数据"""
        with open(os.path.join(DIR_BASE, 'data', file_name), 'r', encoding='utf-8') as f:
            csv_reader = list(csv.reader(f))
            user_lst, passwd_lst = [], []
            for user, passwd in csv_reader:
                user_lst.append(user)
                passwd_lst.append(passwd)

            return user_lst[0], passwd_lst[0]

    def get_baseurl(self, host):
        from conf.operationConfig import OperationConfig
        conf = OperationConfig()
        url = conf.get_section_for_data('api_envi', host)
        return url
